package com.onLineCar.driver.config;

import com.onLineCar.driver.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author The future is full of hope
 * @CreateTime 2023/12/28 0028 11:40
 * @PackageName:com.onLineCar.driver.config
 * @ClassName: Ocr
 */

/**
 * 阿里云官方API
 */
public class Ocr {

    public static void main(String[] args) {
        String host = "https://ocrapi-document.taobao.com";
        String path = "/ocrservice/document";
        String method = "POST";
        String appcode = "5d600475e82a4572a97488d3184ccb64";
        //5d600475e82a4572a97488d3184ccb64   //dsr
        //4f173d9226224ceebebe67b9860cc63a   //dk
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        //String bodys = "{\"img\":\"\",\"url\":\"\",\"prob\":false}";
        String bodys = "{\"url\":\"https://dsr-pic.oss-cn-shanghai.aliyuncs.com/2023/DECEMBER/28/14/15/cd94b5cc-b42b-4030-8c25-9dc23aa2e48e.png\"}";
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String string = EntityUtils.toString(response.getEntity());
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode rootNode = objectMapper.readTree(string);
                String content = rootNode.get("content").asText();
                //去除空格
                String strWithoutSpaces = content.replace(" ", "");
                //消息筛选
                String name = strWithoutSpaces.split("姓名")[1].split("性别")[0];
                String gender = strWithoutSpaces.split("性别")[1].split("民族")[0];
                String nationality = strWithoutSpaces.split("民族")[1].split("出生")[0];
                String birthday = strWithoutSpaces.split("出生")[1].split("住址")[0];
                String address = strWithoutSpaces.split("住址")[1].split("公民身份号码")[0];
                String idNumber = strWithoutSpaces.split("公民身份号码")[1];
                System.out.println("姓名：" + name);
                System.out.println("性别：" + gender);
                System.out.println("民族：" + nationality);
                System.out.println("出生日期：" + birthday);
                System.out.println("住址：" + address);
                System.out.println("公民身份号码：" + idNumber);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
